package com.fare.taluo.application.impl.plane;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.fare.taluo.application.plane.AnalyseService;
import com.fare.taluo.vo.core.PlaneInfo;

@Scope("singleton")
@Service("attentionLineAnalyseServiceImpl")
public class AttentionLineAnalyseServiceImpl extends AnalyseService {

	public List<String> analyse(Map<String, PlaneInfo> firstMap, Map<String, PlaneInfo> secondMap){

		Integer firstPrice = 0;
		PlaneInfo firstInfo = null;
		for(String key: firstMap.keySet()){
			firstInfo = firstMap.get(key);
			if(firstInfo != null)
				firstPrice = Integer.valueOf(((PlaneInfo) firstMap.get(key)).getLowprPrice());
		}
		
		String agent = "";//代理商
		PlaneInfo secondInfo = null;
		Integer secondPrice = 0;
		for(String key: secondMap.keySet()){
			secondInfo = secondMap.get(key);
			if(secondMap.get(key).getAgentInfos().size() == 0) break;
			secondPrice = Integer.valueOf(secondMap.get(key).getAgentInfos().get(0).getPrice());
			agent = secondMap.get(key).getAgentInfos().get(0).getName();
		}
		String finalDiscount = "";
		if(firstPrice != 0 && secondPrice != 0){
			double discount = ((firstPrice+0.0) - (secondPrice+0.0))/(firstPrice+0.0);
			NumberFormat nf = NumberFormat.getInstance();
			nf.setMinimumFractionDigits(2);
			finalDiscount = nf.format(discount * 100) + "%";
		}
		List<String> resultList = new ArrayList<String>();
		if(firstInfo != null){
			resultList.add(firstInfo.getCarrierFullName());
			resultList.add(firstInfo.getFromCity());
			resultList.add(firstInfo.getToCity());
			resultList.add(firstInfo.getFlightBatchDate());
		}else{
			resultList.add(secondInfo.getCarrierFullName());
			resultList.add(secondInfo.getFromCity());
			resultList.add(secondInfo.getToCity());
			resultList.add(secondInfo.getFlightBatchDate());
		}
			resultList.add(String.valueOf(firstPrice));
			resultList.add(String.valueOf(secondPrice));
			resultList.add(finalDiscount);
			resultList.add(agent);
		
		return resultList;
	}
	
	
	public String toJSONByStrs(List<List<String>> resultList,int total){
		String jsonStr = "{\"total\":\""+total+"\","+
						 "\"rows\":[";
		int i = 0;
		for(List<String> row: resultList){
			if(row != null && row.size() != 0){
				i++;
				jsonStr += String.format("{\"aircompany\":\"%s\",\"depcity\":\"%s\",\"arrcity\":\"%s\",\"date\":\"%s\",\"official\":\"%s\",\"qunar\":\"%s\",\"pricescale\":\"%s\",\"agent\":\"%s\"}"
							, row.get(0),row.get(1),row.get(2),row.get(3),row.get(4),row.get(5),row.get(6),row.get(7));
				jsonStr += ",";
			}
		}	
		if(jsonStr.endsWith(","))
			jsonStr = jsonStr.substring(0, jsonStr.length()-1);
		jsonStr += "]}";
		return jsonStr;
	}


	@Override
	public Map<String, List<String>> assembleFusionChartXml(
			Map<String, List<String>> chartMap, String dateStr, String key,
			Map<String, PlaneInfo> infoMap) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
